/*
 * Created on 14 mars 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package jus.aor.talk.ui.server;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

/**
 * @author morat
 *
 */
public class TalkUi extends JFrame implements ServerEventListener {
	private static final long serialVersionUID = -4645206040512584190L;
	protected ServerComposant forum;
	/**
	* réaction à l'évènement Warning de Intervenant 
	*/
	public void warning(ServerEventWarning e){
		writerArea.out.print(e.getWarning()+"\n");
		writerArea.out.flush();
	}
	/**
	* réception de la notification d'un évènement IntervenantEventState1
	*/
	private void gotoState1() {
		startButton.setEnabled(true);
		stopButton.setEnabled(false);
	}
	/**
	* réception de la notification d'un évènement IntervenantEventState2
	*/
	private void gotoState2() {
		startButton.setEnabled(false);
		stopButton.setEnabled(true);
	}

	private javax.swing.JPanel jContentPane = null;
	private javax.swing.JPanel jPanel = null;
	private jus.util.WriterArea writerArea = null;
	private javax.swing.JScrollPane jScrollPane = null;
	private javax.swing.JLabel jLabel = null;
	private javax.swing.JLabel jLabel1 = null;
	private javax.swing.JTextField forumName = null;
	private javax.swing.JTextField registryPort = null;
	private javax.swing.JButton startButton = null;
	private javax.swing.JButton stopButton = null;
	private javax.swing.JPanel jPanel1 = null;
	private javax.swing.JPanel jPanel2 = null;
	private javax.swing.JPanel jPanel3 = null;
	/**
	 * This is the default constructor
	 */
	public TalkUi(ServerComposant forum) {
		super();
		this.forum=forum;
		initialize();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(400,400);
		setLocation(dim.width/2-200,dim.height/2-200);
		gotoState1();
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.pack();
		this.setVisible(true);
		this.addWindowListener(new java.awt.event.WindowAdapter(){
			public void windowClosing(java.awt.event.WindowEvent e) {
				forum.stop();
				System.exit(1);
			}
		});
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new java.awt.BorderLayout());
			jContentPane.add(getJPanel(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanel() {
		if(jPanel == null) {
			jPanel = new javax.swing.JPanel();
			java.awt.GridLayout layGridLayout8 = new java.awt.GridLayout();
			layGridLayout8.setRows(3);
			layGridLayout8.setColumns(1);
			jPanel.setLayout(layGridLayout8);
			jPanel.add(getJPanel2(), null);
			jPanel.add(getJPanel1(), null);
			jPanel.add(getJPanel3(), null);
		}
		return jPanel;
	}
	/**
	 * This method initializes writerArea
	 * 
	 * @return jus.util.WriterArea
	 */
	public jus.util.WriterArea getWriterArea() {
		if(writerArea == null) {
			writerArea = new jus.util.WriterArea();
		}
		return writerArea;
	}
	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private javax.swing.JScrollPane getJScrollPane() {
		if(jScrollPane == null) {
			jScrollPane = new javax.swing.JScrollPane();
			jScrollPane.setViewportView(getWriterArea());
		}
		return jScrollPane;
	}
	/**
	 * This method initializes jLabel
	 * 
	 * @return javax.swing.JLabel
	 */
	private javax.swing.JLabel getJLabel() {
		if(jLabel == null) {
			jLabel = new javax.swing.JLabel();
			jLabel.setText("Forum name");
		}
		return jLabel;
	}
	/**
	 * This method initializes jLabel1
	 * 
	 * @return javax.swing.JLabel
	 */
	private javax.swing.JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new javax.swing.JLabel();
			jLabel1.setText("Registry port");
		}
		return jLabel1;
	}
	/**
	 * This method initializes forumName
	 * 
	 * @return javax.swing.JTextField
	 */
	private javax.swing.JTextField getForumName() {
		if(forumName == null) {
			forumName = new javax.swing.JTextField();
			forumName.setText("ski");
		}
		return forumName;
	}
	/**
	 * This method initializes registryPort
	 * 
	 * @return javax.swing.JTextField
	 */
	private javax.swing.JTextField getRegistryPort() {
		if(registryPort == null) {
			registryPort = new javax.swing.JTextField();
			registryPort.setText("1099");
		}
		return registryPort;
	}
	/**
	 * This method initializes startButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getStartButton() {
		if(startButton == null) {
			startButton = new javax.swing.JButton();
			startButton.setText("Start");
			startButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					forum.start(new String[]{forumName.getText(),registryPort.getText()});
					TalkUi.this.setTitle(forumName.getText());
					TalkUi.this.gotoState2();
				}
			});
		}
		return startButton;
	}
	/**
	 * This method initializes stopButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getStopButton() {
		if(stopButton == null) {
			stopButton = new javax.swing.JButton();
			stopButton.setText("Stop");
			stopButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					forum.stop();
					TalkUi.this.setTitle("");
					TalkUi.this.gotoState1();

				}
			});
		}
		return stopButton;
	}
	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanel1() {
		if(jPanel1 == null) {
			jPanel1 = new javax.swing.JPanel();
			jPanel1.setLayout(new java.awt.BorderLayout());
			jPanel1.add(getJLabel1(), java.awt.BorderLayout.WEST);
			jPanel1.add(getRegistryPort(), java.awt.BorderLayout.CENTER);
		}
		return jPanel1;
	}
	/**
	 * This method initializes jPanel2
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanel2() {
		if(jPanel2 == null) {
			jPanel2 = new javax.swing.JPanel();
			jPanel2.setLayout(new java.awt.BorderLayout());
			jPanel2.add(getJLabel(), java.awt.BorderLayout.WEST);
			jPanel2.add(getForumName(), java.awt.BorderLayout.CENTER);
		}
		return jPanel2;
	}
	/**
	 * This method initializes jPanel3
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanel3() {
		if(jPanel3 == null) {
			jPanel3 = new javax.swing.JPanel();
			jPanel3.add(getStartButton(), null);
			jPanel3.add(getStopButton(), null);
		}
		return jPanel3;
	}
}
